package com.spica.spicaschool.ui.group

import android.view.LayoutInflater
import android.view.ViewGroup
import coil.load
import com.kongzue.dialogx.dialogs.TipDialog
import com.spica.spicaschool.base.BaseFragment
import com.spica.spicaschool.bean.group.GroupDetail
import com.spica.spicaschool.databinding.FragmentGroupDetailBinding
import com.spica.spicaschool.network.Url
import com.spica.spicaschool.utils.SpUtils
import com.spica.spicaschool.utils.TOKEN_SIGN
import rxhttp.wrapper.param.RxHttp
import timber.log.Timber

/**
 * 小组详情
 */
class GroupDetailFragment : BaseFragment<FragmentGroupDetailBinding>() {

  private var id = ""


  override fun setupViewBinding(
    inflater: LayoutInflater,
    container: ViewGroup?
  ): FragmentGroupDetailBinding =
    FragmentGroupDetailBinding.inflate(layoutInflater, container, false)

  override fun init() {
    id = arguments?.getString("Id") ?: ""
    Timber.e(id)
    initDate()
  }


  private fun initDate() {
    RxHttp.postForm(Url.TEAM_INFO)
      .add(
        "token",
        SpUtils.getSpValue(
          context = requireContext(),
          key = TOKEN_SIGN,
          defaultVal = ""
        )
      )
      .add("tid", id)
      .asResponseList(GroupDetail::class.java)
      .subscribe({
        val date = it[0]
        viewBinding.root.post {
          with(date) {
            viewBinding.ivAvatar.load(actAvatar)
            viewBinding.tvGroupDescDetail.text = teamDesc
            viewBinding.tvGroupInfo.text = "创建于${teamTime} · $teamLocation"
            viewBinding.tvGroupName.text = teamName
          }
        }
      }, {
        //请求发生错误
        it.printStackTrace()
        TipDialog.show(it.message)
      })


  }

}